package cn.com.gridinfo.sooc.core.expand.score.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.expand.score.bean.Counts;
import cn.com.gridinfo.sooc.core.expand.score.bean.MyInviteEntity;
import cn.com.gridinfo.sooc.core.expand.score.bean.Rate;
import cn.com.gridinfo.sooc.core.expand.score.bean.Txzh;
import cn.com.gridinfo.sooc.core.expand.score.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;
import cn.com.gridinfo.sooc.utils.MD5;

/**
 * 有关积分的dao
 *
 * @author yingchunli
 */
public class ScoreDao extends SoocIDao {

    public String status;//确认是否成功的信息，  "0": 成功  非0: 不成功
    public String statusCode;//提示信息
    public String data;//数据
    public List<MyInviteEntity> myInviteList;//我的邀请列表
    public List<Counts> countsInfoList = new ArrayList<>();//积分明细列表  提现记录列表

    public int score;//用户总积分
    public int appScore;//用户体验积分
    public int cash;//可提取金额
    public int first;//有没有提过钱 1有  0没有
    public int rate;//汇率  如果100积分=1元，则返回100

    public String zfbzm;//支付宝账号
    public List<Txzh> txzhList;//提现账户信息
    private String count;//我的邀请列表数
    private List<Rate> rateList;//汇率列表

    public ScoreDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        if (result == null) return;
        status = result.get("status").asText();
        statusCode = result.get("statusCode").asText();
        Log.d("result->" + result.toString());
        switch (requestCode) {
            case CodeConst.BIND_COUNT:
                //绑定支付宝成功
                break;
            case CodeConst.GET_MYCOUNTS:
                //获取我的积分成功
                //存储信息
                score = result.get("data").get("score").intValue();
                appScore = result.get("data").get("appScore").intValue();
                cash = result.get("data").get("cash").intValue();
                first = result.get("data").get("first").intValue();
                rate = result.get("data").get("rate").intValue();

                Arad.preferences.putString("score", score + "");
                Arad.preferences.putString("appScore", appScore + "");
                Arad.preferences.putString("cash", cash + "");
                Arad.preferences.putString("first", first + "");
                Arad.preferences.putString("rate", rate + "");
                Arad.preferences.flush();
                Log.d("我的积分信息：score->" + score + "\nappscore->" + appScore + "\ncash->" + cash + "\nfirst->" + first + "\nrate->" + rate);
                break;

            case CodeConst.EXPAND_SCORE_GET_MYACCOUNT:
                //获取兑换账户
                txzhList = JsonUtil.node2pojoList(result.get("data"), Txzh.class);
                if (txzhList.size() > 1) {
                    zfbzm = txzhList.get(0).getZh();//给支付宝账号赋值
                }
                break;

            //积分明细 提现规则  首次请求  下拉请求  上拉请求
            case CodeConst.GET_COUNTS_FIRST:
            case CodeConst.GET_COUNTS_PUSHDOWN:
            case CodeConst.GET_COUNTS_PULLUP:
                countsInfoList = JsonUtil.node2pojoList(result.get("data"), Counts.class);
                Log.d("countsList->" + countsInfoList.size());
                break;

            //获取我的邀请列表
            case CodeConst.GET_MYINVITE_FIRST:
            case CodeConst.GET_MYINVITE_PULLUP:
            case CodeConst.GET_MYINVITE_PUSHDOWN:
                myInviteList = JsonUtil.node2pojoList(result.get("data"), MyInviteEntity.class);
                count = result.get("count").asText();
                break;

            case CodeConst.GET_RULES:
                //提现规则
                data = result.get("data").get("info") + "";
                Log.d("data->" + result.get("data").get("info"));
                break;

            case CodeConst.EXPAND_SCORE_RULE:
                //获取兑换规则
                rateList = JsonUtil.node2pojoList(result.get("data"), Rate.class);
                Log.d("兑换规则->" + result.get("data"));
                break;
        }
    }

    /**
     * 绑定支付宝
     *
     * @param zh          支付宝账号
     * @param zhmc        支付宝实名
     * @param pwd         登录密码
     * @param requestCode 请求码
     */
    public void bindCount(String zh, String zhmc, String pwd, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "bindCount", true);
        RequestParams params = new RequestParams();
        params.add("zhlx", "aliaccount");
        params.add("zh", zh);
        zfbzm = zh;//给支付宝账号赋值
        params.add("zhmc", zhmc);
        params.add("password", MD5.encode(StringUtils.changeSpecialChar(pwd)));
        postRequest(url, params, requestCode);
    }

    /**
     * 我的积分
     *
     * @param requestCode 请求码
     */
    public void getMyCounts(int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "myScore", true);
        Log.d("我的积分：url->" + url);
        RequestParams params = new RequestParams();
        postRequest(url, params, requestCode);
    }

    /**
     * 积分明细
     *
     * @param key         类型 积分明细时，0为全部，1为收入，2为支出。 提现记录时，0为全部，1为审核中，2为失败。3成功
     * @param requestCode 请求码
     */
    public void getCounts(int key, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "scoreDetail", true);
        Log.d("积分明细：url->" + url);
        RequestParams params = new RequestParams();
        params.add("key", key + "");
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 提现记录
     *
     * @param key         0为全部，1为审核中，2为失败。3成功。不传也是全部
     * @param requestCode 请求码
     */
    public void getTxRecords(int key, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "cashDetail", true);
        Log.d("提现记录：url->" + url);
        RequestParams params = new RequestParams();
        params.add("key", key + "");
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 该节点的提现规则
     *
     * @param requestCode 请求码
     */
    public void getTx(int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "cashApply", true);
        Log.d("url->" + url);
        RequestParams params = new RequestParams();
        postRequest(url, params, requestCode);
    }

    /**
     * 获取提现规则
     *
     * @param requestCode 请求码
     */
    public void getTxRules(int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "rules", true);
        Log.d("url->" + url);
        RequestParams params = new RequestParams();
        postRequest(url, params, requestCode);
    }

    /**
     * 获取兑换账户
     *
     * @param requestCode 请求码
     */
    public void getMyAccount(int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "myAccount", true);
        Log.d("url->" + url);
        RequestParams params = new RequestParams();
        params.add("zhlx", "aliaccount");
        postRequest(url, params, requestCode);
    }

    /**
     * 提交增加的积分
     *
     * @param jfz         增加的分数，可以为0
     * @param jfbsm       积分类型
     * @param time        操作时间
     * @param md5         MD5(uid+jfbsm+2jfz+time+(3-10))
     * @param requestCode 请求码
     */
    public void addAcount(int jfz, String jfbsm, String time, String md5, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "myAccount", true);
        Log.d("url->" + url);
        RequestParams params = new RequestParams();
        params.add("jfz", jfz + "");
        params.add("jfbsm", jfbsm);
        params.add("time", time);
        params.add("md5", md5);
        postRequest(url, params, requestCode);
    }

    /**
     * 发送提现
     *
     * @param je          金额
     * @param requestCode 请求码
     */
    public void sendTxIfo(int je, String zfb, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "changeScore", true);
        Log.d("提现：url->" + url);
        RequestParams params = new RequestParams();
        params.add("jfbsm", "User100");
        params.add("je", je + "");
        params.add("zhlx", "aliaccount");
        params.add("zh", zfb);
        Log.d("支付账号" + zfb);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取我的邀请列表
     *
     * @param requestCode 请求码
     */
    public void getMyInviteList(int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "myInvited", true);
        Log.d("获取我的邀请列表：url->" + url);
        RequestParams params = new RequestParams();
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    public String getStatus() {
        return status;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public List<MyInviteEntity> getMyInviteList() {
        return myInviteList;
    }

    public List<Counts> getCountsList() {
        return countsInfoList;
    }

    public int getScore() {
        return score;
    }



    public String getData() {
        return data;
    }

    public List<Txzh> getTxzhList() {
        return txzhList;
    }

    public String getCount() {
        return count;
    }

    public List<Rate> getRateList() {
        return rateList;
    }
}
